package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.common.util.ToolUtil;
import com.central.common.web.Result;
import jodd.util.ArraysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class ShiftRecordsHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    /**
     * 交接触发动作
     * @param app
     * @param type
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {

        if ("shift".equals(type)) {
            String shiftNum = params.get("shiftNum").toString();
            // 班次
            String relay = params.get("relay").toString();
            String shifter = MapUtil.getStr(params, "shifter");
            String successor = MapUtil.getStr(params, "successor");
            Date handoverTime = MapUtil.getDate(params, "handoverTime");
            Date takeOverTime = MapUtil.getDate(params, "takeoverTime");
            String shifterPassword = MapUtil.getStr(params, "shifterPassword");
            String successorPassword = MapUtil.getStr(params, "successorPassword");

            if(ToolUtil.isEmpty(shifter)) {
                return Result.failed("交班人不存在");
            }
            if(ToolUtil.isEmpty(shifterPassword)) {
                return Result.failed("请输入交班人密码！");
            }
            if(ToolUtil.isEmpty(successor)) {
                return Result.failed("请选择接班人！");
            }
            if(ToolUtil.isEmpty(successorPassword)) {
                return Result.failed("请输入接班人密码！");
            }

            // 验证密码
            if (handlerService.valiPassword(shifter, shifterPassword)) {
                return Result.failed("交班人密码不正确，请重新输入！");
            }
            if (handlerService.valiPassword(successor, successorPassword)) {
                return Result.failed("接班人密码不正确，请重新输入！");
            }

            List<Map<String, Object>> shiftList = handlerService
                    .findBySql("select * from bl_shiftRecords where shiftNum='" + shiftNum + "'");
            if (null != shiftList) {
                Map<String, Object> thisShift = shiftList.get(0);

                // 设置当值状态
                thisShift.put("status", "已交班");
                handlerService.update("bl_shiftrecords", thisShift);

                // 设置下一值的信息
                Map<String, Object> nextShift = shiftList.get(0);

                handlerService.addBizNum(app, "bl_shiftRecords", nextShift);
                nextShift.remove("id");

                // 设置交班人
                nextShift.put("shifter", successor);
                // 设置班次
                nextShift.put("relay", getNextRelay(relay));
                // 设置接班时间
                nextShift.put("takeoverTime", handoverTime);
                // 设置状态
                nextShift.put("status", "当班");
                // 设置创建时间
                nextShift.put("createTime", new Date());

                handlerService.insert("bl_shiftrecord", nextShift);
            }
        }
        return "交接成功！";
    }

    /**
     * 获取下一班次名称
     * @param relay
     * @return
     */
    private String getNextRelay(String relay) {
        List<Map<String, Object>> relayList = handlerService
                .findBySql("select name from sys_dict where pid=(select id from sys_dict where code='relay') order by pid");

        if (relayList.isEmpty()) {
            return "";
        }

        String[] relayArr = new String[relayList.size()];

        for (int i = 0; i < relayArr.length; i++) {
            relayArr[i] = relayList.get(i).get("name").toString();
        }

        return relayArr[(ArraysUtil.indexOf(relayArr, relay) + 1 ) % relayArr.length];
    }
}
